package com.example.myapplication.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.utils.MD5Utils;

public class RegisterActivity extends AppCompatActivity {
    private TextView tv_main_title; //标题
    private TextView tv_back; //返回按钮
    private Button btn_register;//注册按钮
    private EditText et_user_name,et_psw,et_psw_again;//三个文本控件
    private String userName,psw,pswAgain;
    private RelativeLayout rl_title_bar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //设置此界面为竖屏
        init();
    }

    private void init() {
        //从标题栏页面布局中获取对应控件
        tv_main_title=(TextView)findViewById(R.id.tv_main_title);
        tv_main_title.setText("注册");
        tv_back=(TextView)findViewById(R.id.tv_back);
        rl_title_bar=(RelativeLayout)findViewById(R.id.title_bar);
        rl_title_bar.setBackgroundColor(Color.TRANSPARENT);
        //从注册也面获取对应的ui控件
        btn_register=(Button)findViewById(R.id.btn_register);
        et_psw=(EditText)findViewById(R.id.et_psw);
        et_user_name=(EditText)findViewById(R.id.et_user_name);
        et_psw_again=(EditText)findViewById(R.id.et_psw_again);

   //现在为控件添加监听
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RegisterActivity.this.finish();
            }
        });
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取输入相应控件中的字符串
                getEditString();
                if (TextUtils.isEmpty(userName)){
                    Toast.makeText(RegisterActivity.this,"请输入用户名",
                    Toast.LENGTH_SHORT).show();
                    return;
                }else if (TextUtils.isEmpty(psw)){
                    Toast.makeText(RegisterActivity.this,"请输入密码",
                            Toast.LENGTH_SHORT).show();
                    return;
                }else if (TextUtils.isEmpty(pswAgain)){
                    Toast.makeText(RegisterActivity.this,"请再次输入密码",
                            Toast.LENGTH_SHORT).show();
                    return;
                }else if (!psw.equals(pswAgain)){
                    Toast.makeText(RegisterActivity.this,"输入的两次密码不一样",
                            Toast.LENGTH_SHORT).show();
                    return;
                }else if (isExistUserName(userName)){
                    Toast.makeText(RegisterActivity.this,"此账户名已经存在",
                            Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    Toast.makeText(RegisterActivity.this,"注册成功",
                            Toast.LENGTH_SHORT).show();
                    saveRegisterInfo(userName,psw);//把用户名和密码用saveReg"isterInfo这个方法保存起来
                    Intent data=new Intent();//注册成功后把用户名传递到LoginActivity.java中
                    data.putExtra("userName",userName);
                    setResult(RESULT_OK,data);//发送数据
                    RegisterActivity.this.finish();
                }

            }
        });
    }

    //获取字符串方法
    private void getEditString() {
        userName=et_user_name.getText().toString().trim();
        psw=et_psw.getText().toString().trim();
        pswAgain=et_psw_again.getText().toString().trim();
    }
    //判断账户名存在方法
    private boolean isExistUserName(String userName) {
        boolean has_userName=false;
        //loginInfo表示文件名，MODE_PRIVATE表示模式为私有的
        SharedPreferences sp=getSharedPreferences("loginInfo",MODE_PRIVATE);
        String spPsw=sp.getString(userName,"");
        if (!TextUtils.isEmpty(spPsw)) {
            has_userName=true;
        }
        return has_userName;
    }
    //保存用户名和密码到SharedPreferernces中
    private void saveRegisterInfo(String userName, String psw) {
        String md5Psw= MD5Utils.md5(psw);//把密码用MD5加密
        //loginInfo表示文件名,无此文件则创造
        SharedPreferences sp=getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();//获取编译器
        editor.putString(userName,md5Psw);
        editor.commit();//提交修改
    }

}
